﻿using CloudApiServer.Class;
using Google.Protobuf.WellKnownTypes;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.X500;
using Org.BouncyCastle.Asn1.X509;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Web.Http;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using static log4net.Appender.RollingFileAppender;

namespace CloudApiServer.Controllers
{
    /// <summary>
    /// 地纬医保接口
    /// </summary>
    [RoutePrefix("mhs5")]
    public class FsiApiShandongDarewayController : ApiController
    {

     
        /// <summary>
        /// 山东地位主接口
        /// </summary>
        /// <returns></returns>
        [Route("siSettleService")]
        [HttpPost]
        public async Task<IHttpActionResult> siSettleService(HttpRequestMessage request)
        {
            string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                try
                {
                    if (FsiDatainfo.stateShandongDarewayApi == false)
                    {
                        throw new Exception("该接口已被控制台停用！请联系管理员！");
                    }
                    string soapResponse = request.Content.ReadAsStringAsync().Result;
                    XDocument xDoc = XDocument.Parse(soapResponse);
                    string arg0 = xDoc.Descendants(XName.Get("pipInvoke", "http://service.communication.service.dareway.com/"))
                                   .Elements("arg0")
                                   .FirstOrDefault()
                                   ?.Value;
                    string arg2 = xDoc.Descendants(XName.Get("pipInvoke", "http://service.communication.service.dareway.com/"))
                                   .Elements("arg2")
                                   .FirstOrDefault()
                                   ?.Value;
                    string arg3 = xDoc.Descendants(XName.Get("pipInvoke", "http://service.communication.service.dareway.com/"))
                                   .Elements("arg3")
                                   .FirstOrDefault()
                                   ?.Value;
                    string arg4 = xDoc.Descendants(XName.Get("pipInvoke", "http://service.communication.service.dareway.com/"))
                                   .Elements("arg4")
                                   .FirstOrDefault()
                                   ?.Value;
                    string arg5 = xDoc.Descendants(XName.Get("pipInvoke", "http://service.communication.service.dareway.com/"))
                                   .Elements("arg5")
                                   .FirstOrDefault()
                                   ?.Value;
                    if (string.IsNullOrEmpty(arg0) || string.IsNullOrEmpty(arg2) || string.IsNullOrEmpty(arg3) || string.IsNullOrEmpty(arg4) || string.IsNullOrEmpty(arg5))
                    {
                        throw new Exception($"传入的xml入参格式解析失败！请求的入参参数存在为空的情况，当前获取到的arg0:{arg0};arg2:{arg2};arg3:{arg3};arg5:{arg4};arg5:{arg5};");
                    }
                    Console.WriteLine($"地纬入参解析arg3主入参:{arg3}\r\n地纬入参解析arg4主入参:{arg4}");
                    DateTime dateTime = DateTime.Now;
                    Random rnd = new Random();
                    switch (arg3)
                    {
                        case "read_card":
                            DataTable dtreadcard= new DataTable();
                            var fsiRequestReadCard = JsonConvert.DeserializeObject<FsiShandongDarewayReadCardIntput>(arg4);
                            string cert_no= fsiRequestReadCard.p_ewm;
                            if (cert_no == "")
                            {
                                cert_no = FsiDatainfo.Defautlcardno;
                            }
                            string mdtrt_cert_type = "01";
                            await conn.OpenAsync();
                            using (MySqlCommand cmd = conn.CreateCommand())
                            {
                                cmd.CommandText = "select * from fsi_PsnInfo a left join fsi_dareway_gj_psninfo b on a.certno=b.certno where a.certno=@certno";
                                cmd.Parameters.AddWithValue("@certno", cert_no);
                                using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                                {
                                    dtreadcard.Load(reader);
                                    if (dtreadcard.Rows.Count == 1)
                                    {
                                        double gjzhye =0; 
                                        if (!dtreadcard.Rows[0].IsNull("balc_gj"))
                                        {
                                            gjzhye = Convert.ToDouble(dtreadcard.Rows[0]["balc_gj"]);
                                        }
                                        var FsiHeaderReadCard = new FsiShandongDarewayReadCardOutput
                                        {
                                            resultcode = 0,
                                            resulttext = "调用成功",
                                            ye = Convert.ToDouble(dtreadcard.Rows[0]["balc"]),
                                            psn_no = dtreadcard.Rows[0]["psn_no"].ToString(),
                                            shbzhm = dtreadcard.Rows[0]["certno"].ToString(),
                                            zhzybz = "0",
                                            jbjgid = "37000000",
                                            bnyzftcj_de = 0,
                                            insutype = dtreadcard.Rows[0]["insutype"].ToString(),
                                            ylrylb =/* await PublicMethod.GetMedicareDict(dtreadcard.Rows[0]["insutype"].ToString(), "insutype")*/"在职",
                                            yfdxbz = "0",
                                            ectoken = "370000ec5q2dukf81m6c01500a00001b1b038b",
                                            insuplc_admdvs = dtreadcard.Rows[0]["insuplc_admdvs"].ToString(),
                                            grbh = dtreadcard.Rows[0]["certno"].ToString(),
                                            dwmc= dtreadcard.Rows[0]["emp_name"].ToString(),
                                            sfzhm = dtreadcard.Rows[0]["certno"].ToString(),
                                            dynamic_code = "4243894447065044198401330000",
                                            mdtrt_cert_type = mdtrt_cert_type,
                                            sbkydbz = "1",
                                            cbdsbh = dtreadcard.Rows[0]["insuplc_admdvs"].ToString(),
                                            dsjgbh = dtreadcard.Rows[0]["insuplc_admdvs"].ToString(),
                                            sbqbye = 0,
                                            csrq = dtreadcard.Rows[0]["brdy"].ToString(),
                                            zhye = Convert.ToDouble(dtreadcard.Rows[0]["balc"]),
                                            rydjid = dtreadcard.Rows[0]["psn_no"].ToString(),
                                            ryid = dtreadcard.Rows[0]["psn_no"].ToString(),
                                            cbxz = "C",
                                            dyrylb = "0",
                                            ydbz = "0",
                                            zfbz = "1",
                                            rqlb = "A",
                                            sbjgbh = "37000000",
                                            gjzhye = gjzhye,
                                            yfdxlb = "0",
                                            multisbj = "0",
                                            bnyzftcj_jb = 0,
                                            xb= dtreadcard.Rows[0]["gend"].ToString(),
                                            ylrylbcode="0",
                                            cbjgmc= await PublicMethod.GetMedicareDivision(dtreadcard.Rows[0]["insuplc_admdvs"].ToString()),
                                            xm= dtreadcard.Rows[0]["psn_name"].ToString(),
                                            sbm= dtreadcard.Rows[0]["insuplc_admdvs"].ToString(),
                                            codetype="001"
                                        };
                                        return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderReadCard)));
                                    }
                                    else
                                    {
                                        throw new Exception($"查询人员信息表失败！查询到的人员信息总数为：{dtreadcard.Rows.Count}");
                                    }
                                }
                            }
                        case "settle_ewmgz":
                            var fsiRequestSettleEwmgz = JsonConvert.DeserializeObject<FsiShandongDarewaySettleEwmgzInput>(arg4);
                            string Jshid = $"1{arg5}{dateTime.ToString("yyMMddHHmmssff")}";
                            List<FsiShandongDarewaySettleEwmgzInputPfypddsItem> list = new List<FsiShandongDarewaySettleEwmgzInputPfypddsItem>();
                            list = fsiRequestSettleEwmgz.p_fypd_ds;
                            await conn.OpenAsync();
                            using (MySqlTransaction transaction = conn.BeginTransaction())
                            {
                                using (MySqlCommand cmd = conn.CreateCommand())
                                {
                                    cmd.Transaction = transaction;
                                    try
                                    {
                                        cmd.CommandText = "INSERT INTO `data`.`fsi_dareway_gz_order` (`yybm`,`p_jshid`, `p_sfczhxf`, `p_ewm`, `p_ectoken`, `p_grzhzf`, `settleinputJson`, `returninputJson`, `orderstate`,`create_time`, `update_time`) " +
                                            "VALUES (@yybm,@p_jshid, @p_sfczhxf, @p_ewm, @p_ectoken, @p_grzhzf,@settleinputJson, @returninputJson, @orderstate,@create_time, @update_time);";
                                        cmd.Parameters.AddWithValue("@yybm", arg5);
                                        cmd.Parameters.AddWithValue("@p_jshid", Jshid);
                                        cmd.Parameters.AddWithValue("@p_sfczhxf", fsiRequestSettleEwmgz.p_sfczhxf);
                                        cmd.Parameters.AddWithValue("@p_ewm", fsiRequestSettleEwmgz.p_ewm);
                                        cmd.Parameters.AddWithValue("@p_ectoken", fsiRequestSettleEwmgz.p_ectoken);
                                        cmd.Parameters.AddWithValue("@p_grzhzf", fsiRequestSettleEwmgz.p_grzhzf);
                                        cmd.Parameters.AddWithValue("@settleinputJson", arg4);
                                        cmd.Parameters.AddWithValue("@returninputJson", "");
                                        cmd.Parameters.AddWithValue("@orderstate", "0");
                                        cmd.Parameters.AddWithValue("@create_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                        cmd.Parameters.AddWithValue("@update_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                        await cmd.ExecuteNonQueryAsync();
                                        cmd.Parameters.Clear();
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception($"订单汇总数据异常：{ex.Message}");
                                    }
                                    string error = "";//商品错误循环拼接
                                    foreach (var item in list)
                                    {
                                        try
                                        {
                                            if (string.IsNullOrEmpty(item.kdysbm)||string.IsNullOrEmpty(item.kdysmc)||string.IsNullOrEmpty(item.zxysbm) || string.IsNullOrEmpty(item.zxysmc))
                                            {
                                                throw new Exception($"开单医生编码{item.kdysbm}|开单医生名称{item.kdysmc}|执行医生编码{item.zxysbm}|执行医生名称{item.zxysmc}存在为空的情况！");
                                            }
                                            cmd.CommandText = "INSERT INTO `data`.`fsi_dareway_gz_order_detail` (`yybm`,`p_jshid`, `yyxmbm`, `yyxmmc`, `gg`, `dw`, `dj`, `sl`, `zje`, `zsm`, `ylxmbm`, zxysbm,zxysmc,kdysbm,kdysmc,clbz,`create_time`, `update_time`) " +
                                                "VALUES (@yybm, @p_jshid, @yyxmbm, @yyxmmc,@gg, @dw, @dj, @sl, @zje, @zsm, @ylxmbm, @zxysbm, @zxysmc, @kdysbm, @kdysmc,@clbz, @create_time, @update_time);";
                                            cmd.Parameters.AddWithValue("@yybm", arg5);
                                            cmd.Parameters.AddWithValue("@p_jshid", Jshid);
                                            cmd.Parameters.AddWithValue("@yyxmbm", item.yyxmbm);
                                            cmd.Parameters.AddWithValue("@gg", item.gg);
                                            cmd.Parameters.AddWithValue("@dw", item.dw);
                                            cmd.Parameters.AddWithValue("@dj", item.dj);
                                            cmd.Parameters.AddWithValue("@sl", item.sl);
                                            cmd.Parameters.AddWithValue("@zje", item.zje);
                                            cmd.Parameters.AddWithValue("@zsm", item.zsm);
                                            cmd.Parameters.AddWithValue("@ylxmbm", item.ylxmbm);
                                            cmd.Parameters.AddWithValue("@zxysbm", item.zxysbm);
                                            cmd.Parameters.AddWithValue("@zxysmc", item.zxysmc);
                                            cmd.Parameters.AddWithValue("@kdysbm", item.kdysbm);
                                            cmd.Parameters.AddWithValue("@kdysmc", item.kdysmc);
                                            cmd.Parameters.AddWithValue("@clbz", item.clbz);
                                            cmd.Parameters.AddWithValue("@create_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                            cmd.Parameters.AddWithValue("@update_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                            await cmd.ExecuteNonQueryAsync();
                                            cmd.Parameters.Clear();
                                        }
                                        catch (Exception ex)
                                        {
                                            error = error + $"商品订单明细商品{item.yyxmmc}异常！订单处理失败：" + ex.Message + "|";
                                        }
                                        cmd.Parameters.Clear();
                                    }
                                    if (error != "")
                                    {
                                        transaction.Rollback();
                                        throw new Exception(error);
                                    }
                                    else
                                    {
                                        transaction.Commit();

                                        var FsiHeaderSettleEwmgz = new FsiShandongDarewaySettleEwmgzOutput
                                        {
                                            jshid = Jshid,
                                            resulttext="调用成功",
                                            resultcode = 0,
                                            trxserno="123321",
                                            outtradeno= $"{dateTime.ToString("yyyyMMddHHmmss")}{rnd.Next(1000000, 10000000)})"
                                        };



                                        return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderSettleEwmgz)));
                                    }
                                }
                            }
                        case "destroy_ewmgz":
                            var fsiRequestDestroyEwmgz = JsonConvert.DeserializeObject<FsiShandongDarewayDestroyEwmgzInput>(arg4);
                            string p_jshid = fsiRequestDestroyEwmgz.p_jshid;
                            DataTable dtEwmgzOrder = new DataTable();
                            await conn.OpenAsync();
                            using (MySqlCommand cmd = conn.CreateCommand())
                            {
                                cmd.CommandText = "select * from fsi_dareway_gz_order where yybm=@yybm and p_jshid=@p_jshid and orderstate=0";
                                cmd.Parameters.AddWithValue("@yybm", arg5);
                                cmd.Parameters.AddWithValue("@p_jshid", p_jshid);
                                using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                                {
                                    dtEwmgzOrder.Load(reader);
                                    cmd.Parameters.Clear();
                                    if (dtEwmgzOrder.Rows.Count == 1)
                                    {
                                        try
                                        {
                                            cmd.CommandText = "update  `data`.`fsi_dareway_gz_order`set returninputJson=@returninputJson, orderstate=@orderstate ,update_time=@update_time where yybm=@yybm and p_jshid=@p_jshid and orderstate=0";
                                            cmd.Parameters.AddWithValue("@yybm", arg5);
                                            cmd.Parameters.AddWithValue("@p_jshid", p_jshid);
                                            cmd.Parameters.AddWithValue("@returninputJson", arg4);
                                            cmd.Parameters.AddWithValue("@orderstate", 1);
                                            cmd.Parameters.AddWithValue("@update_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                            await cmd.ExecuteNonQueryAsync();
                                            cmd.Parameters.Clear();
                                            FsiShandongDarewayErrorOutput FsiHeaderDestroyEwmgz = new FsiShandongDarewayErrorOutput
                                            {
                                                resulttext = "调用成功",
                                                resultcode = 0,
                                            };
                                            return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderDestroyEwmgz)));
                                        }
                                        catch (Exception ex)
                                        {
                                            throw new Exception($"更新订单汇总数据异常：{ex.Message}");
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception($"查询订单数据失败！查询到的未被退款的订单总数为：{dtEwmgzOrder.Rows.Count}");
                                    }
                                }


                            }
                        case "get_zfbl":
                            List<FsiShandongDarewayGetZfblOutputZfbldsItem> GetZfblList = new List<FsiShandongDarewayGetZfblOutputZfbldsItem>();
                            FsiShandongDarewayGetZfblOutputZfbldsItem ZfbldsItem = new FsiShandongDarewayGetZfblOutputZfbldsItem
                            {
                                zfbl = 0,
                                sm = "接口数据模拟"
                            };
                            GetZfblList.Add(ZfbldsItem);
                            //自付比例无逻辑，固定返回0
                            FsiShandongDarewayGetZfblOutput FsiHeaderGetZfbl = new FsiShandongDarewayGetZfblOutput
                            {
                                resulttext = "调用成功",
                                resultcode = 0,
                                zfbl_ds = GetZfblList
                            };
                            return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderGetZfbl)));
                        case "settle_mz_pre":
                            var fsiRequestSettleMzPre = JsonConvert.DeserializeObject<FsiShandongDarewaySettleMzPreInput>(arg4);
                            List<FsiShandongDarewaySettleMzPreInputPfypddsItem> SettleMzPreInputPfypddsItemList = new List<FsiShandongDarewaySettleMzPreInputPfypddsItem>();
                            string mzpreJshid = $"2{arg5}{dateTime.ToString("yyMMddHHmmssff")}";
                            SettleMzPreInputPfypddsItemList = fsiRequestSettleMzPre.p_fypd_ds;

                            double mzprezje = 0;
                            List<FsiShandongDarewaySettleMzPreOutputFyxxdsItem> SettleMzPreOutputFyxxdsList = new List<FsiShandongDarewaySettleMzPreOutputFyxxdsItem>();
                            foreach (var item in SettleMzPreInputPfypddsItemList)
                            {
                                FsiShandongDarewaySettleMzPreOutputFyxxdsItem SettleMzPreOutputFyxxds = new FsiShandongDarewaySettleMzPreOutputFyxxdsItem
                                {
                                    sxh=item.sxh,
                                    zje=item.zje,
                                    zlje=item.zje*(1-item.sxzfbl),
                                    zfje = item.zje * item.sxzfbl,
                                    zfbl= item.sxzfbl,
                                    xj=0,
                                    sfxmdj ="1",
                                    qezfbz = "0",
                                };
                                SettleMzPreOutputFyxxdsList.Add(SettleMzPreOutputFyxxds);
                                mzprezje = mzprezje + item.zje;
                            }


                            var FsiHeaderDarewaySettleMzPre = new FsiShandongDarewaySettleMzPreOutput
                            {
                                resultcode=0,
                                resulttext="调用成功",
                                fyxx_ds= SettleMzPreOutputFyxxdsList,
                                jshid= mzpreJshid,
                                brjsrq=dateTime.ToString("yyyyMMddHHmmss"),
                                zje= mzprezje,
                                ybfdje= Math.Round(mzprezje * (FsiDatainfo.acct_mulaid_pay+FsiDatainfo.fund_pay_sumamt + FsiDatainfo.acct_mulaid_pay), 2),//医保负担(个账+基金+共济)
                                brfdje= Math.Round(mzprezje * FsiDatainfo.psn_cash_pay, 2),//病人负担金额	个人现金支付
                                bcfhjbybfy= FsiDatainfo.inscp_scp_amt,//本次符合基本医保费用金额
                                bcnrtcfw = Math.Round(mzprezje * FsiDatainfo.fund_pay_sumamt, 2),//本次纳入统筹额度
                                tczf = Math.Round(mzprezje * FsiDatainfo.fund_pay_sumamt, 2),
                                tcfwnfd=0,
                                bcnrdejzfw = FsiDatainfo.hifob_pay,
                                dezf = FsiDatainfo.hifob_pay,
                                gwybz= FsiDatainfo.cvlserv_pay,
                                bcnrsbdbfy= FsiDatainfo.hifes_pay,
                                bcsbdblpzf = FsiDatainfo.hifes_pay,
                                bnynrtcfw=0,
                                bnynrdejzfw=0,
                                bnynrsbdbfw=0,
                                bnynrzjzfw=0,
                                bnynrmztcfw=0,
                                bnsbdbljbxje=0,
                                bnzjzljbxje= 0,
                                bngwybzljbxje=0,
                                bnptmzljbxje=0,
                                bnmzmxbljqfx= 0,
                                bcnrfpfw=0,
                                bcnrjzfw = FsiDatainfo.maf_pay,
                                yljmje=0,
                                fprymzjzje= 0,
                                fpthbxje= 0,
                                dbjzje= 0,
                                ztdjbzf= 0,
                                pkryzjzje= 0,
                                bbtnzbxje= 0,
                                bnljmzjzje= 0,
                                bnljthbxje = 0,
                                ljztdjbzf= 0,
                                tsrytcj= 0,
                                syjjzf=0,
                                jgqldyylbt= 0,
                                hmbbxjemln= 0,
                                hmbbxjemlw= 0,
                                hmbbxje= 0,
                                smibxje= 0,
                                qezf= 0,
                                qetc=0,
                                bftc= 0,
                                grzhzf= Math.Round(mzprezje * FsiDatainfo.acct_pay, 2),//个人账户支出,
                                sydgrzhzf = Math.Round(mzprezje * FsiDatainfo.acct_pay, 2)//个人账户支出,
                            };

                            string JsonSettleMzPreOutput = JsonConvert.SerializeObject(FsiHeaderDarewaySettleMzPre);
                            await conn.OpenAsync();
                            using (MySqlTransaction transaction = conn.BeginTransaction())
                            {
                                using (MySqlCommand cmd = conn.CreateCommand())
                                {
                                    cmd.Transaction = transaction;
                                    try
                                    {
                                        cmd.CommandText = "INSERT INTO `data`.`fsi_dareway_mz_order` (`mzJshid`,yybm,`p_blh`, `p_yltclb`, `p_xm`, `p_xb`, `p_grbh`, `p_xzbz`, `p_sbm`, " +
                                            "`p_ewm`, `p_kh`, `p_jbbm`, `p_jyyybm`, `p_sfrzfs`, `p_dzcfjsbz`, `settlepreinputJson`, `settlepreoutputJson`, `settleinputJson`, " +
                                            "`settleoutputJson`, `returninputJson`, `returnoutputJson`,`orderstate`,`create_time`, `update_time`) VALUES (@mzJshid,@yybm,@p_blh, @p_yltclb, @p_xm, @p_xb, @p_grbh, " +
                                            "@p_xzbz, @p_sbm, @p_ewm, @p_kh, @p_jbbm, p_jyyybm,@p_sfrzfs, @p_dzcfjsbz, @settlepreinputJson, @settlepreoutputJson, @settleinputJson," +
                                            " @settleoutputJson, @returninputJson, @returnoutputJson,@orderstate,@create_time, @update_time)";
                                        cmd.Parameters.AddWithValue("@mzJshid", mzpreJshid);
                                        cmd.Parameters.AddWithValue("@yybm", arg5);
                                        cmd.Parameters.AddWithValue("@p_blh", fsiRequestSettleMzPre.p_blh);
                                        cmd.Parameters.AddWithValue("@p_yltclb", fsiRequestSettleMzPre.p_yltclb);
                                        cmd.Parameters.AddWithValue("@p_xm", fsiRequestSettleMzPre.p_xm);
                                        cmd.Parameters.AddWithValue("@p_xb", fsiRequestSettleMzPre.p_xb);
                                        cmd.Parameters.AddWithValue("@p_grbh", fsiRequestSettleMzPre.p_grbh);
                                        cmd.Parameters.AddWithValue("@p_sbm", fsiRequestSettleMzPre.p_sbm);
                                        cmd.Parameters.AddWithValue("@p_ewm", fsiRequestSettleMzPre.p_ewm);
                                        cmd.Parameters.AddWithValue("@p_kh", fsiRequestSettleMzPre.p_kh);
                                        cmd.Parameters.AddWithValue("@p_jbbm", fsiRequestSettleMzPre.p_jbbm);
                                        cmd.Parameters.AddWithValue("@p_jyyybm", fsiRequestSettleMzPre.p_jyyybm);
                                        cmd.Parameters.AddWithValue("@p_sfrzfs", fsiRequestSettleMzPre.p_sfrzfs);
                                        cmd.Parameters.AddWithValue("@p_dzcfjsbz", fsiRequestSettleMzPre.p_dzcfjsbz);
                                        cmd.Parameters.AddWithValue("@settlepreinputJson", arg4);
                                        cmd.Parameters.AddWithValue("@settlepreoutputJson", JsonSettleMzPreOutput);
                                        cmd.Parameters.AddWithValue("@settleinputJson", "");
                                        cmd.Parameters.AddWithValue("@settleoutputJson", "");
                                        cmd.Parameters.AddWithValue("@returninputJson", "");
                                        cmd.Parameters.AddWithValue("@returnoutputJson", "");
                                        cmd.Parameters.AddWithValue("@orderstate", "0");
                                        cmd.Parameters.AddWithValue("@create_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                        cmd.Parameters.AddWithValue("@update_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                        await cmd.ExecuteNonQueryAsync();
                                        cmd.Parameters.Clear();
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception($"订单汇总数据异常：{ex.Message}");
                                    }
                                    string error = "";//商品错误循环拼接
                                    foreach (var item in SettleMzPreInputPfypddsItemList)
                                    {
                                        try
                                        {
                                            if (string.IsNullOrEmpty(item.kdysbm) || string.IsNullOrEmpty(item.kdysmc) || string.IsNullOrEmpty(item.zxysbm) || string.IsNullOrEmpty(item.zxysmc))
                                            {
                                                throw new Exception($"开单医生编码{item.kdysbm}|开单医生名称{item.kdysmc}|执行医生编码{item.zxysbm}|执行医生名称{item.zxysmc}存在为空的情况！");
                                            }
                                            cmd.CommandText = "INSERT INTO `data`.`fsi_dareway_mz_order_detail` (`mzJshid`,`yybm`, `p_blh`, `sxh`, `yyxmbm`, " +
                                                "`yyxmmc`, `ylxmbm`, `gg`, `dw`, `dj`, `sl`, `zje`, `zsm`, `sxzfbl`, `kdksbm`, `kdksmc`, `zxksbm`, `zxksmc`," +
                                                " `fyfssj`, `jbbm`, `mllb`,kdysbm ,kdysmc,zxysbm,zxysmc,clbz,`create_time`, `update_time`) VALUES (@mzJshid,@yybm, @p_blh, @sxh, @yyxmbm, @yyxmmc, @ylxmbm,@gg, @dw," +
                                                " @dj, @sl, @zje, @zsm, @sxzfbl, @kdksbm, @kdksmc, @zxksbm,@zxksmc, @fyfssj, @jbbm, @mllb, @kdysbm, @kdysmc, @zxysbm, @zxysmc,@clbz, @create_time, @update_time)";
                                            cmd.Parameters.AddWithValue("@mzJshid", mzpreJshid);
                                            cmd.Parameters.AddWithValue("@yybm", arg5);
                                            cmd.Parameters.AddWithValue("@p_blh", fsiRequestSettleMzPre.p_blh);
                                            cmd.Parameters.AddWithValue("@sxh", item.sxh);
                                            cmd.Parameters.AddWithValue("@yyxmbm", item.yyxmbm);
                                            cmd.Parameters.AddWithValue("@yyxmmc", item.yyxmmc);
                                            cmd.Parameters.AddWithValue("@ylxmbm", item.ylxmbm);
                                            cmd.Parameters.AddWithValue("@gg", item.gg);
                                            cmd.Parameters.AddWithValue("@dw", item.dw);
                                            cmd.Parameters.AddWithValue("@dj", item.dj);
                                            cmd.Parameters.AddWithValue("@sl", item.sl);
                                            cmd.Parameters.AddWithValue("@zje", item.zje);
                                            cmd.Parameters.AddWithValue("@zsm", item.zsm);
                                            cmd.Parameters.AddWithValue("@sxzfbl", item.sxzfbl);
                                            cmd.Parameters.AddWithValue("@kdksbm", item.kdksbm);
                                            cmd.Parameters.AddWithValue("@kdksmc", item.kdksmc);
                                            cmd.Parameters.AddWithValue("@zxksbm", item.zxksbm);
                                            cmd.Parameters.AddWithValue("@zxksmc", item.zxksmc);
                                            cmd.Parameters.AddWithValue("@fyfssj", item.fyfssj);
                                            cmd.Parameters.AddWithValue("@jbbm", item.jbbm);
                                            cmd.Parameters.AddWithValue("@mllb", item.mllb);
                                            cmd.Parameters.AddWithValue("@kdysbm", item.kdysbm);
                                            cmd.Parameters.AddWithValue("@kdysmc", item.kdysmc);
                                            cmd.Parameters.AddWithValue("@zxysbm", item.zxysbm);
                                            cmd.Parameters.AddWithValue("@zxysmc", item.zxysmc);
                                            cmd.Parameters.AddWithValue("@clbz", item.clbz);
                                            cmd.Parameters.AddWithValue("@create_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                            cmd.Parameters.AddWithValue("@update_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                            await cmd.ExecuteNonQueryAsync();
                                            cmd.Parameters.Clear();
                                        }
                                        catch (Exception ex)
                                        {
                                            error = error + $"商品订单明细商品{item.yyxmmc}异常！订单处理失败：" + ex.Message + "|";
                                        }
                                        cmd.Parameters.Clear();
                                    }
                                    if (error != "")
                                    {
                                        transaction.Rollback();
                                        throw new Exception(error);
                                    }
                                    else
                                    {
                                        transaction.Commit();
                                        return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderDarewaySettleMzPre)));
                                    }
                                }
                            }
                        case "settle_mz":
                            var fsiRequestSettleMz = JsonConvert.DeserializeObject<FsiShandongDarewaySettleMzPreInput>(arg4);
                            List<FsiShandongDarewaySettleMzPreInputPfypddsItem> SettleMzInputPfypddsItemList = new List<FsiShandongDarewaySettleMzPreInputPfypddsItem>();
                            string mzJshid = $"2{arg5}{dateTime.ToString("yyMMddHHmmssff")}";
                            SettleMzPreInputPfypddsItemList = fsiRequestSettleMz.p_fypd_ds;

                            double mzzje = 0;
                            List<FsiShandongDarewaySettleMzPreOutputFyxxdsItem> SettleMzOutputFyxxdsList = new List<FsiShandongDarewaySettleMzPreOutputFyxxdsItem>();
                            foreach (var item in SettleMzInputPfypddsItemList)
                            {
                                FsiShandongDarewaySettleMzPreOutputFyxxdsItem SettleMzPreOutputFyxxds = new FsiShandongDarewaySettleMzPreOutputFyxxdsItem
                                {
                                    sxh = item.sxh,
                                    zje = item.zje,
                                    zlje = item.zje * (1 - item.sxzfbl),
                                    zfje = item.zje * item.sxzfbl,
                                    zfbl = item.sxzfbl,
                                    xj = 0,
                                    sfxmdj = "1",
                                    qezfbz = "0",
                                };
                                SettleMzOutputFyxxdsList.Add(SettleMzPreOutputFyxxds);
                                mzzje = mzzje + item.zje;
                            }


                            var FsiHeaderDarewaySettleMz = new FsiShandongDarewaySettleMzPreOutput
                            {
                                resultcode = 0,
                                resulttext = "调用成功",
                                fyxx_ds = SettleMzOutputFyxxdsList,
                                jshid = mzJshid,
                                brjsrq = dateTime.ToString("yyyyMMddHHmmss"),
                                zje = mzzje,
                                ybfdje = Math.Round(mzzje * (FsiDatainfo.acct_mulaid_pay + FsiDatainfo.fund_pay_sumamt + FsiDatainfo.acct_mulaid_pay), 2),//医保负担(个账+基金+共济)
                                brfdje = Math.Round(mzzje * FsiDatainfo.psn_cash_pay, 2),//病人负担金额	个人现金支付
                                bcfhjbybfy = FsiDatainfo.inscp_scp_amt,//本次符合基本医保费用金额
                                bcnrtcfw = Math.Round(mzzje * FsiDatainfo.fund_pay_sumamt, 2),//本次纳入统筹额度
                                tczf = Math.Round(mzzje * FsiDatainfo.fund_pay_sumamt, 2),
                                tcfwnfd = 0,
                                bcnrdejzfw = FsiDatainfo.hifob_pay,
                                dezf = FsiDatainfo.hifob_pay,
                                gwybz = FsiDatainfo.cvlserv_pay,
                                bcnrsbdbfy = FsiDatainfo.hifes_pay,
                                bcsbdblpzf = FsiDatainfo.hifes_pay,
                                bnynrtcfw = 0,
                                bnynrdejzfw = 0,
                                bnynrsbdbfw = 0,
                                bnynrzjzfw = 0,
                                bnynrmztcfw = 0,
                                bnsbdbljbxje = 0,
                                bnzjzljbxje = 0,
                                bngwybzljbxje = 0,
                                bnptmzljbxje = 0,
                                bnmzmxbljqfx = 0,
                                bcnrfpfw = 0,
                                bcnrjzfw = FsiDatainfo.maf_pay,
                                yljmje = 0,
                                fprymzjzje = 0,
                                fpthbxje = 0,
                                dbjzje = 0,
                                ztdjbzf = 0,
                                pkryzjzje = 0,
                                bbtnzbxje = 0,
                                bnljmzjzje = 0,
                                bnljthbxje = 0,
                                ljztdjbzf = 0,
                                tsrytcj = 0,
                                syjjzf = 0,
                                jgqldyylbt = 0,
                                hmbbxjemln = 0,
                                hmbbxjemlw = 0,
                                hmbbxje = 0,
                                smibxje = 0,
                                qezf = 0,
                                qetc = 0,
                                bftc = 0,
                                grzhzf = Math.Round(mzzje * FsiDatainfo.acct_pay, 2),//个人账户支出,
                                sydgrzhzf = Math.Round(mzzje * FsiDatainfo.acct_pay, 2)//个人账户支出,
                            };

                            string JsonSettleMzOutput = JsonConvert.SerializeObject(FsiHeaderDarewaySettleMz);
                            await conn.OpenAsync();
                            using (MySqlTransaction transaction = conn.BeginTransaction())
                            {
                                using (MySqlCommand cmd = conn.CreateCommand())
                                {
                                    cmd.Transaction = transaction;
                                    try
                                    {
                                        cmd.CommandText = "INSERT INTO `data`.`fsi_dareway_mz_order` (`mzJshid`,`yybm`, `p_blh`, `p_yltclb`, `p_xm`, `p_xb`, `p_grbh`, `p_xzbz`, `p_sbm`, " +
                                            "`p_ewm`, `p_kh`, `p_jbbm`, `p_jyyybm`, `p_sfrzfs`, `p_dzcfjsbz`, `settlepreinputJson`, `settlepreoutputJson`, `settleinputJson`, " +
                                            "`settleoutputJson`, `returninputJson`, `returnoutputJson`,`orderstate`,`create_time`, `update_time`) VALUES (@mzJshid,@yybm,@p_blh, @p_yltclb, @p_xm, @p_xb, @p_grbh, " +
                                            "@p_xzbz, @p_sbm, @p_ewm, @p_kh, @p_jbbm, p_jyyybm,@p_sfrzfs, @p_dzcfjsbz, @settlepreinputJson, @settlepreoutputJson, @settleinputJson," +
                                            " @settleoutputJson, @returninputJson, @returnoutputJson,@orderstate,@create_time, @update_time)";
                                        cmd.Parameters.AddWithValue("@mzJshid", mzJshid);
                                        cmd.Parameters.AddWithValue("@yybm", arg5);
                                        cmd.Parameters.AddWithValue("@p_blh", fsiRequestSettleMz.p_blh);
                                        cmd.Parameters.AddWithValue("@p_yltclb", fsiRequestSettleMz.p_yltclb);
                                        cmd.Parameters.AddWithValue("@p_xm", fsiRequestSettleMz.p_xm);
                                        cmd.Parameters.AddWithValue("@p_xb", fsiRequestSettleMz.p_xb);
                                        cmd.Parameters.AddWithValue("@p_grbh", fsiRequestSettleMz.p_grbh);
                                        cmd.Parameters.AddWithValue("@p_sbm", fsiRequestSettleMz.p_sbm);
                                        cmd.Parameters.AddWithValue("@p_ewm", fsiRequestSettleMz.p_ewm);
                                        cmd.Parameters.AddWithValue("@p_kh", fsiRequestSettleMz.p_kh);
                                        cmd.Parameters.AddWithValue("@p_jbbm", fsiRequestSettleMz.p_jbbm);
                                        cmd.Parameters.AddWithValue("@p_jyyybm", fsiRequestSettleMz.p_jyyybm);
                                        cmd.Parameters.AddWithValue("@p_sfrzfs", fsiRequestSettleMz.p_sfrzfs);
                                        cmd.Parameters.AddWithValue("@p_dzcfjsbz", fsiRequestSettleMz.p_dzcfjsbz);
                                        cmd.Parameters.AddWithValue("@settlepreinputJson", "");
                                        cmd.Parameters.AddWithValue("@settlepreoutputJson", "");
                                        cmd.Parameters.AddWithValue("@settleinputJson", arg4);
                                        cmd.Parameters.AddWithValue("@settleoutputJson", JsonSettleMzOutput);
                                        cmd.Parameters.AddWithValue("@returninputJson", "");
                                        cmd.Parameters.AddWithValue("@returnoutputJson", "");
                                        cmd.Parameters.AddWithValue("@orderstate", "1");
                                        cmd.Parameters.AddWithValue("@create_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                        cmd.Parameters.AddWithValue("@update_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                        await cmd.ExecuteNonQueryAsync();
                                        cmd.Parameters.Clear();
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception($"订单汇总数据异常：{ex.Message}");
                                    }
                                    string error = "";//商品错误循环拼接
                                    foreach (var item in SettleMzPreInputPfypddsItemList)
                                    {
                                        try
                                        {
                                            if (string.IsNullOrEmpty(item.kdysbm) || string.IsNullOrEmpty(item.kdysmc) || string.IsNullOrEmpty(item.zxysbm) || string.IsNullOrEmpty(item.zxysmc))
                                            {
                                                throw new Exception($"开单医生编码{item.kdysbm}|开单医生名称{item.kdysmc}|执行医生编码{item.zxysbm}|执行医生名称{item.zxysmc}存在为空的情况！");
                                            }
                                            cmd.CommandText = "INSERT INTO `data`.`fsi_dareway_mz_order_detail` (`mzJshid`,`yybm`, `p_blh`, `sxh`, `yyxmbm`, " +
                                                "`yyxmmc`, `ylxmbm`, `gg`, `dw`, `dj`, `sl`, `zje`, `zsm`, `sxzfbl`, `kdksbm`, `kdksmc`, `zxksbm`, `zxksmc`," +
                                                " `fyfssj`, `jbbm`, `mllb`,kdysbm ,kdysmc,zxysbm,zxysmc,clbz,`create_time`, `update_time`) VALUES (@mzJshid,@yybm, @p_blh, @sxh, @yyxmbm, @yyxmmc, @ylxmbm,@gg, @dw," +
                                                " @dj, @sl, @zje, @zsm, @sxzfbl, @kdksbm, @kdksmc, @zxksbm,@zxksmc, @fyfssj, @jbbm, @mllb, @kdysbm, @kdysmc, @zxysbm, @zxysmc, @clbz,@create_time, @update_time)";
                                            cmd.Parameters.AddWithValue("@mzJshid", mzJshid);
                                            cmd.Parameters.AddWithValue("@yybm", arg5);
                                            cmd.Parameters.AddWithValue("@p_blh", fsiRequestSettleMz.p_blh);
                                            cmd.Parameters.AddWithValue("@sxh", item.sxh);
                                            cmd.Parameters.AddWithValue("@yyxmbm", item.yyxmbm);
                                            cmd.Parameters.AddWithValue("@yyxmmc", item.yyxmmc);
                                            cmd.Parameters.AddWithValue("@ylxmbm", item.ylxmbm);
                                            cmd.Parameters.AddWithValue("@gg", item.gg);
                                            cmd.Parameters.AddWithValue("@dw", item.dw);
                                            cmd.Parameters.AddWithValue("@dj", item.dj);
                                            cmd.Parameters.AddWithValue("@sl", item.sl);
                                            cmd.Parameters.AddWithValue("@zje", item.zje);
                                            cmd.Parameters.AddWithValue("@zsm", item.zsm);
                                            cmd.Parameters.AddWithValue("@sxzfbl", item.sxzfbl);
                                            cmd.Parameters.AddWithValue("@kdksbm", item.kdksbm);
                                            cmd.Parameters.AddWithValue("@kdksmc", item.kdksmc);
                                            cmd.Parameters.AddWithValue("@zxksbm", item.zxksbm);
                                            cmd.Parameters.AddWithValue("@zxksmc", item.zxksmc);
                                            cmd.Parameters.AddWithValue("@fyfssj", item.fyfssj);
                                            cmd.Parameters.AddWithValue("@jbbm", item.jbbm);
                                            cmd.Parameters.AddWithValue("@mllb", item.mllb);
                                            cmd.Parameters.AddWithValue("@kdysbm", item.kdysbm);
                                            cmd.Parameters.AddWithValue("@kdysmc", item.kdysmc);
                                            cmd.Parameters.AddWithValue("@zxysbm", item.zxysbm);
                                            cmd.Parameters.AddWithValue("@zxysmc", item.zxysmc);
                                            cmd.Parameters.AddWithValue("@clbz", item.clbz);
                                            cmd.Parameters.AddWithValue("@create_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                            cmd.Parameters.AddWithValue("@update_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                            await cmd.ExecuteNonQueryAsync();
                                            cmd.Parameters.Clear();

                                        }
                                        catch (Exception ex)
                                        {
                                            error = error + $"商品订单明细商品{item.yyxmmc}异常！订单处理失败：" + ex.Message + "|";
                                        }
                                        cmd.Parameters.Clear();
                                    }
                                    if (error != "")
                                    {
                                        transaction.Rollback();
                                        throw new Exception(error);
                                    }
                                    else
                                    {
                                        transaction.Commit();
                                        return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderDarewaySettleMz)));
                                    }
                                }
                            }
                        case "destroy_mz":
                            var fsiRequestDestroymz = JsonConvert.DeserializeObject<FsiShandongDarewayDestroyEwmgzInput>(arg4);
                            string mzp_jshid = fsiRequestDestroymz.p_jshid;
                            DataTable dtmzOrder = new DataTable();
                            await conn.OpenAsync();
                            using (MySqlCommand cmd = conn.CreateCommand())
                            {
                                cmd.CommandText = "select * from fsi_dareway_mz_order where yybm=@yybm and mzJshid=@mzJshid and orderstate=1";
                                cmd.Parameters.AddWithValue("@yybm", arg5);
                                cmd.Parameters.AddWithValue("@mzJshid", mzp_jshid);
                                using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                                {
                                    dtmzOrder.Load(reader);
                                    cmd.Parameters.Clear();
                                    if (dtmzOrder.Rows.Count == 1)
                                    {
                                        try
                                        {
                                            cmd.CommandText = "update  `data`.`fsi_dareway_mz_order`set returninputJson=@returninputJson, orderstate=@orderstate ,update_time=@update_time where yybm=@yybm and mzJshid=@mzJshid and orderstate=1";
                                            cmd.Parameters.AddWithValue("@yybm", arg5);
                                            cmd.Parameters.AddWithValue("@mzJshid", mzp_jshid);
                                            cmd.Parameters.AddWithValue("@returninputJson", arg4);
                                            cmd.Parameters.AddWithValue("@orderstate", 2);
                                            cmd.Parameters.AddWithValue("@update_time", dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
                                            await cmd.ExecuteNonQueryAsync();
                                            cmd.Parameters.Clear();
                                            FsiShandongDarewayErrorOutput FsiHeaderDestroymz = new FsiShandongDarewayErrorOutput
                                            {
                                                resulttext = "调用成功",
                                                resultcode = 0,
                                            };
                                            return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderDestroymz)));
                                        }
                                        catch (Exception ex)
                                        {
                                            throw new Exception($"更新订单汇总数据异常：{ex.Message}");
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception($"查询订单数据失败！查询到的未被退款且结算状态为结算的订单总数为：{dtmzOrder.Rows.Count}");
                                    }
                                }


                            }
                        case "query_gjzh":
                            DataTable dtquerygjzh = new DataTable();
                            var fsiRequestQuerygjzh = JsonConvert.DeserializeObject<FsiShandongDarewayQueryGjzhInput>(arg4);
                            string gjzhJshid = $"3{arg5}{dateTime.ToString("yyMMddHHmmssff")}";
                            await conn.OpenAsync();
                            using (MySqlCommand cmd = conn.CreateCommand())
                            {
                                cmd.CommandText = "select * from fsi_PsnInfo a  join fsi_dareway_gj_psninfo b on a.certno=b.certno where a.certno=@certno";
                                cmd.Parameters.AddWithValue("@certno", fsiRequestQuerygjzh.p_sfzhm);
                                using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                                {
                                    dtquerygjzh.Load(reader);
                                    cmd.Parameters.Clear();
                                    if (dtquerygjzh.Rows.Count == 1)
                                    {
                                        double gjzhye = 0;
                                        if (!dtquerygjzh.Rows[0].IsNull("balc_gj"))
                                        {
                                            gjzhye = Convert.ToDouble(dtquerygjzh.Rows[0]["balc_gj"]);
                                        }
                                        var FsiHeaderQueryGjzh = new FsiShandongDarewayQueryGjzhOutput
                                        {
                                            resulttext = "调用成功",
                                            resultcode = 0,
                                            sptddh = gjzhJshid,
                                            gjzhxm= dtquerygjzh.Rows[0]["psn_name_gj"].ToString(),
                                            gjsfzhm= dtquerygjzh.Rows[0]["certno_gj"].ToString(),
                                            gjcbdbm= dtquerygjzh.Rows[0]["insuplc_admdvs_gj"].ToString(),
                                            gjzhye= gjzhye,
                                            bz= dtquerygjzh.Rows[0]["memo"].ToString()
                                        };
                                        return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderQueryGjzh)));
                                    }
                                    else
                                    {
                                        throw new Exception($"查询共济人员信息表失败！查询到的人员信息总数为：{dtquerygjzh.Rows.Count}");
                                    }
                                }
                            }
                        //case "settle_gjzh":
                        //    DataTable dtquerymzorder = new DataTable();
                        //    var fsiRequestSettleGjzh = JsonConvert.DeserializeObject<FsiShandongDarewaySettleGjzhInput>(arg4);
                        //    await conn.OpenAsync();
                        //    using (MySqlCommand cmd = conn.CreateCommand())
                        //    {
                        //        cmd.CommandText = "select * from fsi_dareway_mz_order where yybm=@yybm and mzJshid=@mzJshid and orderstate=1";
                        //        cmd.Parameters.AddWithValue("@yybm", arg5);
                        //        cmd.Parameters.AddWithValue("@mzJshid", fsiRequestSettleGjzh.p_jshid);
                        //        using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                        //        {
                        //            dtquerymzorder.Load(reader);
                        //            cmd.Parameters.Clear();
                        //            if (dtquerymzorder.Rows.Count == 1)
                        //            {
                        //                double gjzhye = 0;
                        //                if (!dtquerygjzh.Rows[0].IsNull("balc_gj"))
                        //                {
                        //                    gjzhye = Convert.ToDouble(dtquerygjzh.Rows[0]["balc_gj"]);
                        //                }
                        //                var FsiHeaderQueryGjzh = new FsiShandongDarewayQueryGjzhOutput
                        //                {
                        //                    resulttext = "调用成功",
                        //                    resultcode = 0,
                        //                    sptddh = gjzhJshid,
                        //                    gjzhxm = dtquerygjzh.Rows[0]["psn_name_gj"].ToString(),
                        //                    gjsfzhm = dtquerygjzh.Rows[0]["certno_gj"].ToString(),
                        //                    gjcbdbm = dtquerygjzh.Rows[0]["insuplc_admdvs_gj"].ToString(),
                        //                    gjzhye = gjzhye,
                        //                    bz = dtquerygjzh.Rows[0]["memo"].ToString()
                        //                };
                        //                return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeaderQueryGjzh)));
                        //            }
                        //            else
                        //            {
                        //                throw new Exception($"查询门诊订单记录信息表失败！查询到的结算状态为结算的门诊信息表总数为：{dtquerymzorder.Rows.Count}");
                        //            }
                        //        }
                        //    }
                        default:
                            throw new Exception($"[山东地纬结算接口]接口{arg3}未被定义，请检查入参或联系管理员！");
                    }
                }
                catch (Exception ex)
                {
                    string Errnum = LogHelper.GetErrornumber();
                    var FsiHeader = new FsiShandongDarewayErrorOutput
                    {
                        resultcode = -1,
                        resulttext = $"[{Errnum}]调用山东地纬医保自定义服务失败！请检查！错误内容：{ex.Message} 异常来源：{ex.StackTrace}"
                    };
                    return Ok(PublicMethod.DarewayOutputXML(JsonConvert.SerializeObject(FsiHeader)));
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        await conn.CloseAsync();
                    }
                }
            }
        }
    }
}

